package com.backend.teamapi.controller;

import com.backend.teamapi.model.Team;
import com.backend.teamapi.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/teams")
public class TeamController {

    @Autowired
    private TeamService teamService;

    // 创建团队
    @PostMapping("/create")
    public Team createTeam(@RequestBody TeamRequest teamRequest) {
        return teamService.createTeam(teamRequest.getTeamName(), teamRequest.getDescription(), teamRequest.getCreatorId());
    }

    // 获取团队信息（通过团队名称）
    @GetMapping("/{teamName}")
    public Team getTeam(@PathVariable String teamName) {
        return teamService.getTeamByName(teamName);
    }

    // 获取某个用户创建的所有团队
    @GetMapping("/user/{creatorId}")
    public List<Team> getTeamsByCreator(@PathVariable Long creatorId) {
        return teamService.getTeamsByCreator(creatorId);
    }

    // 获取某个用户参与的所有团队
    @GetMapping("/user/participated/{userId}")
    public List<Team> getTeamsByUser(@PathVariable Long userId) {
        return teamService.getTeamsByUser(userId);
    }

    // 添加成员到团队（只有创建者才能添加成员）
    @PostMapping("/{teamId}/addMember")
    public String addMemberToTeam(@PathVariable Long teamId, 
                                  @RequestBody MemberRequest memberRequest) {
        boolean success = teamService.addMemberToTeam(teamId, memberRequest.getUserId(), memberRequest.getOperatorId());
        return success ? "Member added successfully" : "Failed to add member (only creator can add members).";
    }

    // 从团队移除成员（只有创建者才能移除成员，或者成员自己退出）
    @PostMapping("/{teamId}/removeMember")
    public String removeMemberFromTeam(@PathVariable Long teamId, 
                                       @RequestBody MemberRequest memberRequest) {
        boolean success = teamService.removeMemberFromTeam(teamId, memberRequest.getUserId(), memberRequest.getOperatorId());
        return success ? "Member removed successfully" : "Failed to remove member (only creator or member themselves can remove).";
    }

    // 删除团队（只有创建者才能删除团队）
    @DeleteMapping("/{teamId}")
    public String deleteTeam(@PathVariable Long teamId, @RequestBody OperatorRequest operatorRequest) {
        boolean success = teamService.deleteTeam(teamId, operatorRequest.getOperatorId());
        return success ? "Team deleted successfully" : "Failed to delete team (only creator can delete).";
    }

    // 请求体中的类
    public static class TeamRequest {
        private String teamName;
        private String description;
        private Long creatorId;

        // getters and setters
        public String getTeamName() {
            return teamName;
        }

        public void setTeamName(String teamName) {
            this.teamName = teamName;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public Long getCreatorId() {
            return creatorId;
        }

        public void setCreatorId(Long creatorId) {
            this.creatorId = creatorId;
        }
    }

    public static class MemberRequest {
        private Long userId;
        private Long operatorId;

        // getters and setters
        public Long getUserId() {
            return userId;
        }

        public void setUserId(Long userId) {
            this.userId = userId;
        }

        public Long getOperatorId() {
            return operatorId;
        }

        public void setOperatorId(Long operatorId) {
            this.operatorId = operatorId;
        }
    }

    public static class OperatorRequest {
        private Long operatorId;

        // getters and setters
        public Long getOperatorId() {
            return operatorId;
        }

        public void setOperatorId(Long operatorId) {
            this.operatorId = operatorId;
        }
    }
}
